local app = require 'app'
local game = app.game.running

local settlement = game.ui.element()

settlement.store_watchers = 
{
    ingame = 
    {
        ['$'] = function()

        end,

        ['游戏结算'] = function(self, store, state)
            self:show_detail(state.over_detail)
        end
    }
}

function settlement:ctor(ingame)
    self.res = 'club.threecard.doudizhu/ui/ingame.b:settlement'
    self.ingame =  ingame
end

function settlement:show_detail(detail)
    if not self.loaded then
        self:load()
    end

    self:lazy('do_show_detail', detail)
end

function settlement:do_show_detail(detail)
    self:show()
    local seats = self.ingame.seats
    local find_seat = function(no)
        for i = 1, seats.max do
            if seats[i].player and seats[i].player.seat.no == no then
                return seats[i]
            end
        end
    end

    game.gamelib.log.error('明牌')
    for _, info in ipairs(detail.remain_card_ids) do
        local seat = find_seat(info.no)
        if seat then
            if seat.local_no ~= 1 then
                seat.poker_container:mingpai(info.card_ids)
            end
        else
            game.gamelib.log.error('没有找到seat可能已经走了 no:'..info.no)
        end
    end
end

return settlement